<template>
	<view class="small-title">
		<view class="upload-image-view">
			<view>{{first}}</view>
			<image :class="firstImg=='/static/idCardFirst.png' ? 'image-view' : 'image-view-border'" :src="firstImg" mode="" @tap='chooseImage1'></image>
			<image :class="secondImg=='/static/idCardSecond.png' ? 'image-view' : 'image-view-border'" :src="secondImg" mode="" @tap='chooseImage2'></image>
		</view>
		<view class="upload-image-view">
			<view style="margin-left: 27rpx;"></view>
			<text class="image-view-title">身份证正面</text>
			<text class="image-view-title">身份证反面</text>
		</view>
	</view>
</template>

<script>
	import {
		myUploadFile
	} from "../../util/network.js"
	var sourceType = [
		['camera'], //拍照
		['album'], //相册
		['camera', 'album'] //拍照或相册
	]
	var sizeType = [
		['compressed'], //压缩
		['original'], //原图
		['compressed', 'original'] //压缩或原图
	]
	export default {
		name:"access-card-img",
		data() {
			return {
				index: this.first,
				firstImgIn: this.firstImg,
				secondImgIn: this.secondImg,
				isChangeIn:this.isChange,
			};
		},
		props: {
			first: {
				type: String,
				default: ''
			},
			firstImg:{
				type:String
			},
			secondImg:{
				type:String
			},
			isChange:{
				type:Boolean,
				default:false
			}
		},
		methods: {
			addImage(index, position, value, imgId){
				console.log("[info]","调试身份证上传图片，我是子组件"+index+"  position："+position)
				this.$emit("addImg",index - 1, position, value, imgId)
			},
		//选择图片
			chooseImage1: async function() {
				if(this.isChangeIn) {
					uni.chooseImage({
						count: 1,
						sourceType: sourceType[2],
						// #ifdef MP-WEIXIN
						sizeType: sizeType[1],
						// #endif
						success: (res) => {
							
							let imgId = 0
							let a = myUploadFile(uni.getStorageSync('token'), res.tempFilePaths[0])
							 a.then((v)=>{
								 imgId = v
								 this.firstImgIn = res.tempFilePaths[0];
								 this.addImage(this.index, 1, this.firstImgIn, imgId);
							})

						}
					})
				}else{
					uni.previewImage({
						current: 0,
						urls: [this.firstImg]
					})
				}
			},
			chooseImage2: async function() {
				if(this.isChangeIn) {
					uni.chooseImage({
						count: 1,
						sourceType: sourceType[2],
						// #ifdef MP-WEIXIN
						sizeType: sizeType[1],
						// #endif
						success: (res) => {
							let imgId = 0
							let a = myUploadFile(uni.getStorageSync('token'), res.tempFilePaths[0])
							 a.then((v)=>{
								 imgId = v
								 console.log("得到文件id为：", imgId)
								 this.secondImgIn = res.tempFilePaths[0];
								 this.addImage(this.index, 2, this.secondImgIn, imgId);
							})
						}
					})
				}else{
					uni.previewImage({
						current: 0,
						urls: [this.secondImg]
					})
				}
			}
				//预览图片
				// previewImage: function(e) {
				// 	var current = e.target.dataset.src
				// 	uni.previewImage({
				// 		current: current,
				// 		urls: this.imageList
				// 	})
				// },
				// //删除图片
				// deleteImage: function(e) {
				// 	var index = e.target.dataset.index;
				// 	var that = this;
				// 	var images = that.imageList;
				// 	images.splice(index, 1);
				// 	that.imageList = images;
				// }
		}
	}
</script>

<style>
	@import "../../static/css/upload-imgs.css";
	.small-title{
		margin: 20rpx 0 20rpx 60rpx;
	}
</style>
